﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NHoangUtilities.NHoangControls
{
    public partial class NHPagingControl : System.Web.UI.UserControl
    {
        public int TotalPage { get; set; }
        private int currentPage;
        protected void Page_Load(object sender, EventArgs e)
        {
            lbTotalPageNumber.Text = TotalPage.ToString(); ;
            getCurrentPage();
            if (currentPage > TotalPage)
                Response.Redirect(HttpContext.Current.Request.Url.AbsolutePath.ToString());           
            HidePageLink();
            setLinkForControl();
            
        }

        private void setLinkForControl()
        {
            if (currentPage == 1)
            {
                setURLforPage(1, "1", lbtCurrentPage);
            }
            else if (currentPage == 2)
            {
                setURLforPage(1, "1", lbtPrePage1);
                setURLforPage(2, "2", lbtCurrentPage);
            }
            else if (currentPage == 3)
            {
                setURLforPage(1, "1", lbtPrePage2);
                setURLforPage(2, "2", lbtPrePage2);
                setURLforPage(3, "3", lbtCurrentPage);
            }
            else if (currentPage == 4)
            {
                setURLforPage(1, "Trang Đầu", lbtFirstPage);
                setURLforPage(2, "2", lbtPrePage2);
                setURLforPage(3, "3", lbtPrePage1);
                setURLforPage(4, "4", lbtCurrentPage);
            }
            else
            {
                setURLforPage(1, "Trang Đầu", lbtFirstPage);
                setURLforPage(currentPage - 2, (currentPage-2).ToString(), lbtPrePage2);
                setURLforPage(currentPage - 1, (currentPage - 1).ToString(), lbtPrePage1);
                setURLforPage(currentPage, currentPage.ToString(), lbtCurrentPage);
            }
            int k = TotalPage - currentPage;
            if (k == 0)
            {
                lbtNextPage1.Visible = false;
                lbtNextPage2.Visible = false;
                lbtLastPage.Visible = false;

            }
            else if (k == 1)
            {
                setURLforPage(currentPage + 1, currentPage + 1 + "", lbtNextPage1);
            }
            else if (k == 2)
            {
                setURLforPage(currentPage + 1, currentPage + 1 + "", lbtNextPage1);
                setURLforPage(currentPage + 2, currentPage + 2 + "", lbtNextPage2);
            }
            else if (k == 3)
            {
                setURLforPage(currentPage + 1, currentPage + 1 + "", lbtNextPage1);
                setURLforPage(currentPage + 2, currentPage + 2 + "", lbtNextPage2);
                setURLforPage(currentPage + 3, currentPage + 3 + "", lbtLastPage);
            }
            else
            {
                setURLforPage(currentPage + 1, currentPage + 1 + "", lbtNextPage1);
                setURLforPage(currentPage + 2, currentPage + 2 + "", lbtNextPage2);
                setURLforPage(TotalPage, "Trang Cuối", lbtLastPage);
            }

        }      
        private void getCurrentPage()
        {
            System.Collections.Specialized.NameValueCollection queryList = Request.QueryString;
            if (queryList == null)
            {
                currentPage = 1;
                return;
            }
            if (queryList.Count == 0)
            {
                currentPage = 1;
                return;
            }
            if (queryList.AllKeys.Contains("p"))
            {
                try
                {
                    currentPage = int.Parse(queryList["p"].ToString());
                }
                catch
                {
                    Response.Redirect(HttpContext.Current.Request.Url.AbsolutePath.ToString());           
                }

            }
            else
            {
                currentPage = 1;
            }
        }     
        private void HidePageLink()
        {
            lbtFirstPage.Visible = false;
            lbtPrePage2.Visible = false;
            lbtPrePage1.Visible = false;
            lbtCurrentPage.Visible = false;
            lbtNextPage1.Visible = false;
            lbtNextPage2.Visible = false;
            lbtLastPage.Visible = false;
        }
        #region set url
        private void setURLforPage(int page, string name, HyperLink hyperL)
        {
            hyperL.Text = name;
            hyperL.Visible = true;
            System.Collections.Specialized.NameValueCollection queryList = Request.QueryString;
            string currentURL = HttpContext.Current.Request.Url.AbsolutePath.ToString();
            string url = currentURL;
            if (queryList == null)
            {
                url = url + "?p=" + page;
            }
            else if (queryList.Count == 0)
            {
                url = url + "?p=" + page;
            }
            else if (queryList.AllKeys.Contains("p"))
            {

                List<string> keyList = queryList.AllKeys.ToList();
                keyList.Remove("p");
                if (keyList.Count == 0)
                {
                    url = url + "?p=" + page;
                }
                else
                {
                    url += "?" + keyList[0] + "=" + queryList[keyList[0]];
                    for (int i = 1; i < keyList.Count; i++)
                    {
                        url += "&" + keyList[i] + "=" + queryList[keyList[i]];
                    }
                    url += "&p=" + page;
                }
            }
            else
            {
                url = HttpContext.Current.Request.Url.ToString() + "&p=" + page;
            }
            hyperL.NavigateUrl = url;
        }
        #endregion

       

        
    }
}